var WxSearch = require('../../wxSearch/wxSearch.js');
var app = getApp();
var common = require('../common/common.js');
var url = app.globalData.url;
var error = common.error;
Page({
  data: {
    open: true,
    select_all: true,
    locIDs:null,
    selDel: '全选',
    inputValue:null,
    list: []
  },
  onLoad: function(data) {
    var that = this;
    var currentTab = that.data.currentTab;
    console.log(currentTab);
    // 页面初始化，接收登录页面所传来的参数
    var user = wx.getStorageSync('account');
    var pwd = wx.getStorageSync('password');
    var Token = wx.getStorageSync('Token');
    var CurentTime = common.CurentTime();
    var params2 = {
      Token: Token,
      WithSalesData: true,
      WithComparedData: true,
    }
    //请求店铺列表
    common.requestLoading(url + '/shops', params2, '正在加载中', function(res) {
      console.log(res)
      var newArr = res;
      var listShopId = [];
      for (var i = 0; i < res.length; i++) {
        res[i].checked = true;
        listShopId.push(res[i].LocationID); 
      };
      console.log(listShopId)
      var dayShopIdStr = listShopId.join("|");
      wx.setStorageSync("dayShopIdStr", dayShopIdStr)
      console.log(res)
       //进入默认页面locIDs是全选
      var locIDs = [];
      for (let i = 0; i < res.length; i++) {
        locIDs[i] = res[i].LocationID
      }
      that.setData({
        list: res,
        locIDs: locIDs
      })
      var listShop = that.data.list
      var shopName = []
      for (var i = 0; i < listShop.length; i++) {
        shopName[i] = listShop[i].LocationName
      }
      //初始化的时候渲染wxSearchdata 第二个为你的search高度
      WxSearch.init(that, 43, shopName);
      WxSearch.initMindKeys(['weappdev.com', '微信小程序开发', '微信开发', '微信小程序']);         
    }, error) 
  },
  wxSearchFn: function(e) {
    var that = this
    WxSearch.wxSearchAddHisKey(that);
    var inputValue = that.data.inputValue
    var list = that.data.list
    console.log(that.data.list)
    console.log(inputValue)
    for (var i = 0; i < list.length;i++){
      if (inputValue == list[i].LocationName){
        var listId = list[i].LocationID
      }
      return listId
    }
    console.log(listId)
  },
  wxSearchInput: function(e) {
    var that = this
    WxSearch.wxSearchInput(e, that);
    console.log(e.detail.value)
    that.setData({
      inputValue: e.detail.value
    })
    
  },
  wxSerchFocus: function(e) {
    var that = this;
    var list = that.data.list
    WxSearch.wxSearchFocus(e, that);
    console.log(e)
    that.setData({
      open: false,
      inputValue: e.detail.value
    })
    console.log(that.data.inputValue)

  },
  wxSearchBlur: function(e) {
    var that = this
    WxSearch.wxSearchBlur(e, that);
    that.setData({
      open: true
    })
  },
  wxSearchKeyTap: function(e) {
    var that = this
    WxSearch.wxSearchKeyTap(e, that);
  },
  wxSearchDeleteKey: function(e) {
    var that = this
    WxSearch.wxSearchDeleteKey(e, that);
  },
  wxSearchDeleteAll: function(e) {
    var that = this;
    WxSearch.wxSearchDeleteAll(that);
  },
  wxSearchTap: function(e) {
    var that = this
    WxSearch.wxSearchHiddenPancel(that);
  },
  //点击店铺列表
  // listShop: function(e) {
  //   console.log(e)
  //   wx.switchTab({
  //     url: "../Home/home"
  //   });

  // },
  onShareAppMessage: function(e) {
    return {
      title: '',
      path: '/page/user?id=123' //（路径，即是你在app.json中注册的路径，每个页面均需要填写这个，才会出现分享?id= 123为参数部分）
    }
  },

  /**
   * 点击按钮全选
   * 
   */
  select_all: function() {
    let that = this;
    that.setData({
      select_all: !that.data.select_all
    })
    if (that.data.select_all) {
      let arr = that.data.list;
      let arr2 = [];
      var locIDs = [];
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].checked == true) {
          arr2.push(arr[i]);
        } else {
          arr[i].checked = true;
          arr2.push(arr[i]);
        }
      }
      for(let i = 0;i<arr2.length;i++){
        locIDs[i] = arr2[i].LocationID
      }
      console.log(locIDs)
      that.setData({
        list: arr,
        locIDs: locIDs
        //middlearr: arr2
        //selDel: '取消全选'
      })
    }
  },
  /**
   * 取消按钮全选
   */
  // 取消全选
  select_none: function() {
    let that = this;
    let locIDs = that.data.locIDs;
    that.setData({
      select_all: !that.data.select_all
    })
    let arr = that.data.list;
    let arr2 = [];
    for (let i = 0; i < arr.length; i++) {
      arr[i].checked = false;
      arr2.push(arr[i]);
    }
    that.setData({
      list: arr,
      locIDs:[]
    })
  },
  /**
   * 确定按钮点击
   * 1.判断是否全选select_all
   * 2.locIDs默认是全列表ids >=1
  */
  
  confirm:function(e){
    let that = this;
    console.log(that.data.list)
    //var locIDs = [];
    var listChecked = that.data.list;
      console.log(that.data.locIDs && shops == false)
      console.log(!that.data.select_all && this.data.locIDs !== null)
      if (!that.data.select_all && this.data.locIDs.length <= 0) {
      wx.showModal({
        title: '提示',
        content: '请选择店铺',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      var shops = that.data.locIDs;
        var shopsHtml = that.data.list;
        console.log(shops) //null
        console.log(shopsHtml) //null
        if (shops !== null){
          var shopsData = shops.join("|");
        }  
      //储存shopId值
      wx.setStorageSync("dayShopIdStr", shopsData)
      wx.switchTab({
        url: '/pages/Home/home',
        success:function(){
          var page = getCurrentPages().pop();
          if (page == undefined || page == null) return;
            page.onLoad();
        } 
      })
    
  }
  },
  checkboxChange: function (e) {
    var that = this
    console.log(e)
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    var locIDs1 = e.detail.value
    console.log(locIDs1);
    var list = that.data.list;
    console.log(list)

    var LocationID = []
    for (var i = 0; i < list.length;i++){
      LocationID[i] = list[i].LocationID
    }
    if (LocationID.length == locIDs1.length){
      that.setData({
        select_all: true,
      })
    }
    if (locIDs1.length == 0 || LocationID.length > locIDs1.length){
      console.log('nihaohao')
      that.setData({
        select_all : false,
      })
    }
    that.setData({
      locIDs: locIDs1
    })
    
  }




})